<template>
  <div class="match_table" :style="{ height: height }">
    
    <!-- <el-scrollbar class="mscroll"> -->
    <el-table :data="tableData" style="width: 100%" height="calc( 100% - 44px )">
      <el-table-column prop="match" label="所属赛事" width="180px">
        <template #default="{ row }">
          <a :href="getRowLink(row)" class="row-link">
            <span style="font-weight: normal !important;" class="match-text">{{ row.league_name }}</span>
          </a>
        </template>
      </el-table-column>
      <el-table-column prop="time" label="时间" align="center">
        <template #default="{ row }">
          <a :href="getRowLink(row)" class="row-link">
            <span style="font-weight: bold;">{{ extractTime(row.match_time) }}</span>
          </a>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center" width="75px">
        <template #default="{ row }">
          <a :href="getRowLink(row)" class="row-link">
            <!-- matchtype == 'record' ||  -->
            <span v-if="matchtype == 'record'"
              style="display: flex; justify-content: center; align-items: center;">
              <img src="@/assets/table/icon-over.png" style="margin-right: 8px" />
              完
            </span>
            <!-- matchtype == 'early' -->
            <span v-if="matchtype == 'early'">{{ row.state_desc || row.live_time_str }}</span>
            <!-- matchtype == 'live' -->
            <div v-if="matchtype == 'live'" class="status-time" style="color: #ff3b30; font-weight: bold;">
              <img :src="geticon(row.live_time_str)" style="margin-right: 8px;" />
              {{ row.live_time_str }}
              <span :class="['breathing', { fade: !isVisible }]"> '</span>
            </div>
          </a>
        </template>
      </el-table-column>
      <el-table-column prop="home" label="主场球队" align="right">
        <template #default="{ row }">
          <a :href="getRowLink(row)" class="row-link">
            <span v-if="row.red_card && parseInt(row.red_card.split(':')[0]) > 0" class="red">{{
              parseInt(row.red_card.split(":")[0]) }}</span>
            <span v-if="row.yellow_card && parseInt(row.yellow_card.split(':')[0]) > 0" class="yellow">{{
              parseInt(row.yellow_card.split(":")[0]) }}</span>
            <!-- <span style="color: #8e8e93; font-size: 12px; margin-right: 5px">{{ row.hwin}}</span>
              <span style="font-weight: normal !important;"> {{ row.NCN?.TEAM_H }}</span> -->
            <span style="font-size: 14px;color: #000;font-weight: normal;font-family: AlibabaPuHuiTi_2_55_;">{{
              row.home_name }}</span>
          </a>
        </template>
      </el-table-column>
      <el-table-column prop="point" label="比分" align="center" width="60px">
        <template #default="{ row }">
          <a :href="getRowLink(row)" class="row-link">
            <span style="color: #e92937; font-weight: bold">
              {{ row.score }}
            </span>
          </a>
        </template>
      </el-table-column>
      <el-table-column prop="guest" label="客场球队" align="left">
        <template #default="{ row }">
          <a :href="getRowLink(row)" class="row-link">
            <span style="font-size: 14px;color: #000;font-weight: normal;font-family: AlibabaPuHuiTi_2_55_;">{{
              row.away_name }}</span>
            <!-- <span  style="font-weight: normal !important;"> {{ row.NCN?.TEAM_C }}</span>
              <span style="color: #8e8e93; font-size: 12px; margin: 0 5px">{{ row.gwin}}</span> -->
            <span v-if="row.red_card && parseInt(row.red_card.split(':')[1]) > 0" class="red">{{
              parseInt(row.red_card.split(":")[1])
              }}</span>
            <span v-if="row.yellow_card && parseInt(row.yellow_card.split(':')[1]) > 0" class="yellow">{{
              parseInt(row.yellow_card.split(":")[1]) }}</span>
          </a>
        </template>
      </el-table-column>
      <el-table-column v-if="matchtype == 'live'" prop="half" label="半场" align="center" width="70px">
        <template #default="{ row }">
          <a :href="getRowLink(row)" class="row-link">
            <span style="font-weight: bold;">
              {{ row.half_score }}
            </span>
          </a>
        </template>
      </el-table-column>
      <el-table-column v-if="matchtype == 'live'" prop="corner" label="角球" align="center" width="70px">
        <template #default="{ row }">
          <a :href="getRowLink(row)" class="row-link">
            <img src="@/assets/detail/jiaoqiu.png" style="margin-right: 5px;" />
            <span style="font-weight: bold;">
              {{ row.corner }}
            </span>
          </a>
        </template>
      </el-table-column>
      <el-table-column v-if="matchtype == 'live' || matchtype == 'early'"
          prop="trend"
          label="初始走势"
          align="center"
          width="200px"
        >
          <template #default="{row}" >
            <template v-if="row.odds_early.AH.hrfsp != ''">
            <div class="row-link" style="padding: 0 10px">
              <el-row :gutter="20">
                <el-col class="trtext" :span="8">{{ row.odds_early?.AH?.hrfsp }}</el-col>
                <el-col class="trtext" :span="8" style="color: green;">{{ row.odds_early?.AH?.hrf }}</el-col>
                <el-col class="trtext" :span="8">{{ row.odds_early?.AH?.grfsp }}</el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col class="trtext" :span="8">{{ row.odds_early?.OU?.hdxsp }}</el-col>
                <el-col class="trtext" :span="8" style="color: #e92937;">{{ row.odds_early?.OU?.hdx }}</el-col>
                <el-col class="trtext" :span="8">{{ row.odds_early?.OU?.hdxsp }}</el-col>
              </el-row>
            </div>
            </template>
            <template v-else>
              <div style="font-weight: 400">暂无数据</div>
            </template>
          </template>
        </el-table-column>
      <el-table-column label="功能" align="center" width="85px">
        <template #default="{ row }">
          <div class="function">
            <div @click="(event) => toggleStart(row, event)" class="icon">
              <img :src="row.is_favor == 1 ? startRed : start" />
            </div>
            <!-- <div @click="(event) => toggleTop(row, event)" class="icon">
              <img :src="row.topActive ? topRed : top" />
            </div> -->
          </div>
        </template>
      </el-table-column>
    </el-table>
    <app-pagination :total="total" :current-page="currentPage" :page-size="pageSize"
      @update:currentPage="emit('update:currentPage', $event)" @update:pageSize="emit('update:pageSize', $event)" />
    <!-- </el-scrollbar> -->

  </div>
</template>
<script setup>
import startRed from '@/assets/table/start_red.png'
import start from '@/assets/table/start.png'
import topRed from '@/assets/table/top_red.png'
import top from '@/assets/table/top.png'
import icon15 from '@/assets/table/icon-15.png'
import icon30 from '@/assets/table/icon-30.png'
import icon45 from '@/assets/table/icon-45.png'
import icon60 from '@/assets/table/icon-60.png'
import icon75 from '@/assets/table/icon-75.png'
import icon90 from '@/assets/table/icon-90.png'
import icon90a from '@/assets/table/icon-90+.png'


import { ElMessage } from 'element-plus';
import { favorAdd, favorDelete, favorLive, favorUpLive } from '../../../api/match';
import { onMounted, onUnmounted } from 'vue';
import { color } from 'echarts'
const props = defineProps({
  data: Object,
  total: Number,
  currentPage: Number,
  pageSize: Number,
  height: String,
  matchtype: String
});
// const emit = defineEmits(['update:currentPage', 'update:pageSize']);
// const onPageChange = (page) => {
//   currentPage.value = page; // 更新当前页
// };

// const onPageSizeChange = (size) => {
//   pageSize.value = size; // 更新每页条目数
//   currentPage.value = 1; // 重置为第一页
// };
const tableData = computed(() => {
  return props.data || []; // 如果 props.data 存在，返回它，否则返回一个空数组
})
const getRowLink = (row) => {
  
  return `${location.hash}/${props.matchtype}/${row.match_id}`

}
const emit = defineEmits(['update:currentPage', 'update:pageSize'])

const route = useRoute();
const toggleStart = (row, event) => {
  event.stopPropagation(); // 阻止事件冒泡
  if (row.is_favor === 0) {
    makeFavorTodo({ mid: row.match_id }).then(() => {
      ElMessage.success('已收藏')
      row.is_favor = 1
    })
  }
  if (row.is_favor === 1) {
    makeFavorDelete({ mid: row.match_id }).then(() => {
      row.FA = 0;
      ElMessage.success('取消收藏')
      row.is_favor = 0
    })
  }
};
const geticon = (time) => {
  if (time > 0 && time <= 15) {
    return icon15
  } else if (time > 15 && time <= 30) {
    return icon30
  } else if (time > 30 && time <= 45) {
    return icon45
  } else if (time > 45 && time <= 60) {
    return icon60
  } else if (time > 60 && time <= 75) {
    return icon75
  } else if (time > 75 && time <= 90) {
    return icon90
  } else if (time > 90) {
    return icon90a
  }
}


const toggleTop = (row, event) => {
  event.stopPropagation(); // 阻止事件冒泡
  // 如果是第一次点击置顶，则保存该行的原始索引
  if (!row.topActive && row.originalIndex === undefined) {
    row.originalIndex = tableData.value.indexOf(row); // 保存原始位置
  }

  // 切换置顶状态
  row.topActive = !row.topActive;

  if (row.topActive) {
    // 置顶：将点击的 row 移到第一位
    const index = tableData.value.indexOf(row);
    if (index > -1) {
      tableData.value.splice(index, 1); // 从原位置移除
      tableData.value.unshift(row); // 将其添加到第一位
    }
  } else {
    // 取消置顶：将 row 移回原始位置
    const originalIndex = row.originalIndex; // 获取保存的原始索引
    if (originalIndex !== undefined) {
      tableData.value.splice(tableData.value.indexOf(row), 1); // 从置顶位置移除
      tableData.value.splice(originalIndex, 0, row); // 插入到原始索引位置
    }
    delete row.originalIndex; // 删除原始位置的记录
  }
};


const isVisible = ref(true);
let intervalId;


const extractTime = (dateTime) => {
  if (dateTime != '' && dateTime != undefined) {
    const timePart = dateTime.split(' ')[1];
    const [hour, minute, second] = timePart.split(':');

    return `${hour}:${minute}:${second}`
  } else {
    return ''
  }


}
onMounted(() => {
  // 每两秒切换一次可见性
  intervalId = setInterval(() => {
    isVisible.value = !isVisible.value;
  }, 1000); // 每2秒切换一次可见性
});



onUnmounted(() => {
  // 清除定时器
  clearInterval(intervalId);
});
</script>
<style lang='scss' scoped>
.match-text {
  // // word-wrap: break-word;
  // width: 100%;
  // display: block;
  // overflow: hidden;
  // // 
  // white-space:normal;
  // text-overflow: ellipsis;
  display: block;
  /* 使span充当块级元素 */
  width: 150px;
  overflow: hidden;
  /* 隐藏溢出的内容 */
  white-space: nowrap;
  /* 不换行 */
  text-overflow: ellipsis;
  /* 超出部分用省略号表示 */
  text-align: left;
}

.row-link {
  display: block;
  /* 让链接填满单元格 */
  width: 100%;
  /* 确保宽度为100% */
  text-decoration: none;
  /* 去除下划线 */
  color: inherit;
  /* 继承颜色 */
}

// .row-link:hover {
//   background-color: rgba(255, 59, 48, 0.1); /* 悬停时的背景色 */
// }
.match_table {
  z-index: 1000;
}

.mscroll {
  height: 100%;
}

.breathing {
  transition: opacity 1s ease;
  /* 设置过渡效果 */
}

.fade {
  opacity: 0;
  /* 透明度为0，隐藏元素 */
}

.el-table {
  --el-table-header-bg-color: #f8f9fa;
  border-radius: 5px;
}

.red {
  background-color: #e92937;
  border-radius: 3px;
  color: #fff;
  padding: 0 2px;
  margin: 0 5px;
}

.yellow {
  background-color: #ff9000;
  border-radius: 3px;
  color: #fff;
  padding: 0 2px;
  margin: 0 5px;
}

.trtext {
  // margin: 0 4px;
  font-size: 11px;
  font-weight: bold;
}

.trtext1 {
  color: green;
}

.trtext2 {
  color: #e92937;
}

.function {
  display: flex;
  justify-content: center;
  /* 水平居中 */
  align-items: center;
  /* 垂直居中 */
}

.icon {
  margin: 0 5px;
}

.status-time {
  display: flex;
  justify-content: center;
  align-items: center;
}

.under {
  width: 1376px;
  height: 1px;
  margin: 14px 0;
  border-radius: 4px;
  background-color: #ebebeb;
}
</style>
<style>
.match_table .el-table th {
  height: 48px;
  line-height: 48px;
  font-size: 14px;

}

/* 表头 */
.match_table .el-table td {
  height: 48px;
  border-bottom: 1px solid #ebebeb;
}

/* 表格 */
.match_table .el-table .cell {
  padding: 0;
  font-weight: normal;
  color: #000;
  font-size: 14px;
  margin-left: 16px;
  font-weight: bold;
}

.match_table .el-table .el-table__cell {
  padding: 0;
}


.match_table .el-table .el-table__row:hover {
  background-color: #e0e0e0 !important;
  /* 鼠标悬停时的颜色 */
}

.match_table .el-scrollbar__wrap {
  background: #fff;

}
</style>
